package com.df.springmvc.system.manage.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import com.df.springmvc.common.bean.CurrentUserBean;
import com.df.springmvc.common.bean.MsgBean;
import com.df.springmvc.common.util.WebUtil;
import com.df.springmvc.supper.GlobalVariable;

public class CheckLogin implements Filter{
	/**
	 * @param args
	 */
	public static void main(String[] args){
		// TODO Auto-generated method stub
	}
	@Override
	public void init(FilterConfig filterConfig) throws ServletException{
		// TODO Auto-generated method stub
	}
	@Override
	public void doFilter(ServletRequest arg0,ServletResponse arg1,FilterChain arg2) throws IOException,ServletException{
		// TODO Auto-generated method stub
		CurrentUserBean currentUserBean=null;
		ArrayList<String> pathList=new ArrayList<String>();
		pathList.add("scripts/player/player.jsp");
		HttpServletRequest request=(HttpServletRequest)arg0;
		HttpServletResponse response=(HttpServletResponse)arg1;
		String path=request.getContextPath();
		String basePath=request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
		String loginPath=request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/" + "page/system/manage/Login/";
		String page=basePath.replace(path + "/","") + request.getRequestURI();
		GlobalVariable.basePath=basePath;
		request.setAttribute("basePath",GlobalVariable.basePath);
		Logger.getLogger(getClass()).info("Page:" + page);
		try{
			Object temp=request.getSession().getAttribute("CurrentUserBean");
			if(temp != null){
				currentUserBean=(CurrentUserBean)temp;
				arg2.doFilter(request,response);
			}else if(page.equals(basePath + "page/system/manage/Login/")){
				org.apache.log4j.Logger.getLogger(getClass()).info("用户IP:[" + request.getRemoteAddr() + "]---进入登录页面");
				arg2.doFilter(request,response);
			}else if(page.equals(basePath + "page/system/manage/Login/doLogin")){
				org.apache.log4j.Logger.getLogger(getClass()).info("用户IP:[" + request.getRemoteAddr() + "]---登录系统");
				arg2.doFilter(request,response);
			}else if(page.equals(basePath + "page/system/manage/Login/toGetPassword")){
				org.apache.log4j.Logger.getLogger(getClass()).info("用户IP:[" + request.getRemoteAddr() + "]---进入密码找回页面");
				arg2.doFilter(request,response);
			}else if(page.equals(basePath + "page/system/manage/Login/doRetrievePassword")){
				org.apache.log4j.Logger.getLogger(getClass()).info("用户IP:[" + request.getRemoteAddr() + "]---密码找回");
				arg2.doFilter(request,response);
			}else if(page.equals(basePath + "page/system/manage/Login/doSendSMS")){
				org.apache.log4j.Logger.getLogger(getClass()).info("用户IP:[" + request.getRemoteAddr() + "]---密码找回发送手机验证码");
				arg2.doFilter(request,response);
			}else if(page.contains(basePath + "page/workflow/api/API")){
				arg2.doFilter(request,response);
			}else{
				Enumeration<String> enumeration=request.getHeaderNames();
				while(enumeration.hasMoreElements()){
					String key=enumeration.nextElement();
					// System.out.println(key + ":" + request.getHeader(key));
				}
				if("XMLHttpRequest".equals(request.getHeader("x-requested-with"))){
					WebUtil.json(response,new MsgBean("用户登录已经失效",false));
				}else{
					org.apache.log4j.Logger.getLogger(getClass()).info("用户IP:[" + request.getRemoteAddr() + "]---未登录");
					// arg2.doFilter(request,response);
					WebUtil.script(response,"top.location='" + loginPath + "';");
					// response.sendRedirect(basePath + "");
				}
			}
		}catch(Exception e){
			// TODO: handle exception
			e.printStackTrace();
			// response.sendRedirect("system_Login.do");
		}
	}
	@Override
	public void destroy(){
		// TODO Auto-generated method stub
	}
}
